import 'dart:ui';

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_deer/database/beans/config/print_style_config.dart';
import 'package:flutter_deer/database/beans/config/res_zero_tube_entity.dart';
import 'package:flutter_deer/database/beans/order/order_list_bean_entity.dart';
import 'package:flutter_deer/database/tables.dart';
import 'package:flutter_deer/generated/res_upload_oil_entity.dart';
import 'package:flutter_deer/mvvm/base_net_provider.dart';
import 'package:flutter_deer/net/dio_utils.dart';
import 'package:flutter_deer/net/http_api.dart';
import 'package:flutter_deer/util/print/data/print_link_type.dart';
import 'package:flutter_deer/util/toast_utils.dart';

class CommonHttpProvider extends BaseNetProvider {
  // 补单
  void supplyOil({
    required int orderNo,
    required int amount,
    required void Function() success,
  }) {
    final AppOrderBean order = AppOrderBean.repairOrder(orderNo, amount);

    asyncRequestNetwork<ResUploadOilEntity>(
      Method.post,
      url: HttpApi.repairOrder,
      params: order.toUpLoadJson(),
      onSuccess: (data) {
        print('补单成功 data: $data');
        order.energyOrderId = data?.energyOrderId ?? '';
        AppOrderBean.insertOrder(order);
        success();
      },
      onError: (code, msg) {
        Toast.show('补单失败 code: $code, msg: $msg');
        print('补单失败 code: $code, msg: $msg');
      },
    );
  }

  // 获取零管配置
  void getZeroTube() {
    asyncRequestNetwork<AppZeroModConfig>(
      Method.get,
      url: HttpApi.getZeroTube,
      onSuccess: (data) {
        data?.save();
      },
      onError: (code, msg) {
        Toast.show('获取零管配置失败 code: $code, msg: $msg');
      },
    );
  }

  // 获取合并油单
  void getMergeOilList() {
    // asyncRequestNetwork<AppZeroModConfig>(
    //   Method.post,
    //   url: HttpApi.getUnpaidOrderList,
    //   params: {
    //     'pageNo': 1,
    //     'pageSize': 100,
    //     'shelfNo': 1,
    //     'mchGoodsId': 1,
    //     'shiftNo': 1,
    //     'mchCode': '10000000000000000000000000000000',
    //     'energyOrderStatus': 1,
    //     'energyOrderStatusList': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    //     'dataHide': 1,
    //   },
    //   onSuccess: (data) {
    //     print('获取合并油单成功 data: $data');
    //   },
    //   onError: (code, msg) {
    //     Toast.show('获取合并油单失败 code: $code, msg: $msg');
    //     print('获取合并油单失败 code: $code, msg: $msg');
    //   },
    // );

    // 走本地数据库吧
    // final query = AppOrderBean.queryUnPayOrderMerge([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
  }

  // 打印配置
  void getPrintStyle(String configKey, {VoidCallback? successCallback}) {
    requestNetwork<PrintStyleConfig>(
      isShow: false,
      Method.get,
      url: HttpApi.getTicketConfig,
      queryParameters: {
        'configKey': configKey,
      },
      onSuccess: (data) {
        SpUtil.putString(configKey, data?.toJson().toString() ?? '');
        PrintLinkType.save(configKey, data!);
        successCallback?.call();
      },
      onError: (code, msg) {
        Toast.show('获取打印配置失败 code: $code, msg: $msg');
        print('获取打印配置失败 code: $code, msg: $msg');
      },
    );
  }

  // 打印
  void printTicket(String orderId, {required void Function(Map<String, dynamic>) success}) {
    requestNetwork<Map<String, dynamic>>(Method.post, url: HttpApi.printOrder, params: {
      'orderId': orderId,
    }, onSuccess: (data) {
      success(data!);
    }, onError: (code, msg) {
      Toast.show('打印失败 code: $code, msg: $msg');
    });
  }

  // 获取订单列表
  void getOrderList({
    required int pageNo,
    int pageSize = 10,
    int? orderType,
    int? orderStatus,
    String offsetId = '',
    required void Function(OrderListBeanEntity) success,
  }) {
    asyncRequestNetwork<OrderListBeanEntity>(
      Method.post,
      url: HttpApi.getOrderList,
      params: {
        'pageNo': pageNo,
        'pageSize': pageSize,
        //查询时间类型 1 今天 2 当前班次 3 本周 4 本月 5 自定义时间
        // 'timeType': 1,
        // 'shelfNo': 1,
        // 'phone': SpUtil.getString('phone', defValue: ''),
        //订单的状态 全部的话不传
        if (orderType != null) 'orderTypes': orderType,
        //订单列表类型包含：1全部、2消费、3充值
        // 'orderTypes': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        // 'offsetId': 0,
        if (orderStatus != null) 'orderStatus': [orderStatus],
      },
      onSuccess: (data) {
        success(data!);
      },
      onError: (code, msg) {
        Toast.show('获取订单列表失败 code: $code, msg: $msg');
      },
    );
  }

  // 校验操作密码
  void checkOperationPassword({
    required String password,
    required void Function(String) success,
    required void Function(int, String) onError,
  }) {
    asyncRequestNetwork<String>(
      Method.post,
      url: HttpApi.checkOperationPassword,
      params: {
        'password': password,
      },
      onSuccess: (data) {
        success(data ?? '');
      },
      onError: (code, msg) {
        Toast.show('校验操作密码失败 code: $code, msg: $msg');
        onError(code, msg);
      },
    );
  }

  // 订单退款
  void orderRefund({
    required String orderId,
    required String password,
    required void Function(String) success,
    required void Function(int, String) onError,
  }) {
    asyncRequestNetwork<String>(
      Method.post,
      url: HttpApi.orderRefund,
      params: {
        'operationPassword': password,
        'orderId': orderId,
      },
      onSuccess: (data) {
        success(data ?? '');
      },
      onError: (code, msg) {
        Toast.show('订单退款失败 code: $code, msg: $msg');
        onError(code, msg);
      },
    );
  }
}
